package com.rue89;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

/**
 * Liste contenant un seul article complet
 *
 */
public class ArticleAdapter extends BaseAdapter {

	private Context mContext;
	private Article article;

	public ArticleAdapter(Context context) {

		article = null;
		mContext = context;
	}

	
	
	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}



	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	@Override
	public int getCount() {

		return 1;
		
	}

	@Override
	public Object getItem(int position) {
		return article;
	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = null;
		view = View.inflate(mContext, R.layout.article, null);
		
		// WebView contenu = (WebView)view.findViewById(R.id.contenuarticle);
		
		TextView contenu = (TextView)view.findViewById(R.id.contenuarticle);
		TextView auteur = (TextView)view.findViewById(R.id.auteurarticle);
		ImageView image = (ImageView)view.findViewById(R.id.imagearticle);
		TextView titre = (TextView)view.findViewById(R.id.titrearticle);
				
		contenu.setText(Html.fromHtml(article.getContenu()));
		// contenu.loadData(article.getContenu(), "text/html", "utf-8");
		// contenu.getSettings().setPluginsEnabled(true);
		
		auteur.setText(Html.fromHtml(article.getAuteur()));
		titre.setText(Html.fromHtml(article.getTitre()));
		
		contenu.setTextSize(Float.valueOf(Tools.getTailleTexte(mContext)));
		//auteur.setTextSize(Float.valueOf(Tools.getTailleTexte(mContext)));
		//titre.setTextSize(Float.valueOf(Tools.getTailleTexte(mContext)));
	    
		// Chargement de l'image
	    try {
	    	image.setScaleType(ScaleType.CENTER_CROP);
	        
	        
	        if (article.getImageUrl() == null) {
	        	image.setImageDrawable((BitmapDrawable)mContext.getResources().getDrawable(R.drawable.temp));
	        }
	        else {
	        	image.setImageDrawable(Tools.downloadImage(article.getImageUrl(), article.getId(), mContext));
	        }
	        	
	        } catch(Exception e ) {
	        	Log.e("ListeArticleCompletAdapter.getView", e.getMessage(),e);
	        }
		
		return view;

	}
	
	public void setArticle(Article article) {
		this.article = article;
	}
	
	

}
